<?php

use console\db\channels\TransferTableMigration;
use yii\helpers\HtmlPurifier;
use yii\db\Query;

/**
 * 文章评论转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300010_000008_t_cms_article_comment extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public $sourceDb = 'olddb';

    /**
     * @inheritdoc
     */
    public function validateTransfer(&$data) {
        if (!parent::validateTransfer($data)) {
            return false;
        }
        $data['content'] = HtmlPurifier::process($data['content'], [
            'HTML.Allowed' => '',
            'AutoFormat.RemoveEmpty' => true
        ]);
        $article = (new Query)->from('{{%cms_article}}')->where([
            'id' => $data['article_id']
        ])->one();
        if (!empty($article)) {
            return true;
        }
        return false;
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'cms_article_comment' => 'ts_article_comment',
            ],
            'columns' => [
                'id',
                'article_id',
                'comment_id',
                'user_id',
                'post' => 'content',
                'status' => function($data) {
                    if ($data['status'] < 0) {
                        return 0;
                    }
                    return 10;
                },
                'created_at',
                'updated_at',
                'deleted_at' => function($data) {
                    if ($data['status'] < 0) {
                        return $data['updated_at'];
                    }
                    return 0;
                },
            ],
        ];
    }

}
